package com.ufo.web;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class IPUtil {
    static{
        InetAddress ia = null;
        try {
            ia = InetAddress.getLocalHost();
            localHostAddress= ia.getHostAddress();//获取计算机IP
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
    public static String localHostAddress;


    public static String getIpAddress(HttpServletRequest req){
        String ip=req.getHeader("x-forwarded-for");
        if(ip==null || ip.length() == 0||"unknown".equalsIgnoreCase(ip)){
            ip=req.getHeader("Proxy-Client-IP");
        }
        if(ip==null || ip.length() == 0||"unknown".equalsIgnoreCase(ip)){
            ip=req.getHeader("WL-Proxy-Client-IP");
        }
        if(ip==null || ip.length() == 0||"unknown".equalsIgnoreCase(ip)){
            ip=req.getHeader("HTTP-CLIENT-IP");
        }
        if(ip==null || ip.length() == 0||"unknown".equalsIgnoreCase(ip)){
            ip=req.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if(ip==null || ip.length() == 0||"unknown".equalsIgnoreCase(ip)){
            ip=req.getRemoteAddr();
        }
        return ip;
    }
}
